package com.atguigu02.filestream;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * ClassName: FileStreamTest
 * Description:字节流
 *
 * @Author LIU
 * @Create 2025/3/27 21:12
 * @Version 1.0
 */

public class FileStreamTest {
    /*
     * 需求：复制一份playgirl.jpg文件，命名为playgirl_copy.jpg
     *
     * */
  @Test
  public void test1(){
    FileInputStream fis = null;
    FileOutputStream fos = null;

      try {
          //创建对象
          File src = new File("playgirl.jpg");
          File dest = new File("playgirl_copy.jpy");

          fis = new FileInputStream(src);
          fos = new FileOutputStream(dest);

          //输入内容
          byte[] cbuffer = new byte[1024];
          int len;
          while((len = fis.read(cbuffer)) !=-1){
              fos.write(cbuffer, 0, len);
          }
          System.out.println("复制成功");
      } catch (IOException e) {
          throw new RuntimeException(e);
      } finally {
          try {
              if(fos !=null){
                  fos.close();
              }
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
          try {
              if(fis !=null){
                  fis.close();
              }
          } catch (IOException e) {
              throw new RuntimeException(e);
          }
      }
  }
    /*
     * 需求：复制一份hello.txt文件，命名为hello_copy1.txt
     *
     *  可以使用字节流实现文本文件的复制。
     * */
    @org.junit.Test
    public void test2() {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            //1. 创建相关的File类的对象
            File srcFile = new File("hello.txt");
            File destFile = new File("hello_copy1.txt");

            //2. 创建相关的字节流
            fis = new FileInputStream(srcFile);
            fos = new FileOutputStream(destFile);

            //3. 数据的读入和写出
            byte[] buffer = new byte[5];
            int len;//记录每次读入到buffer中字节的个数
            while ((len = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
            }
            System.out.println("复制成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4. 关闭资源
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
    /*
     * 需求：读取hello.txt文件，将数据显示在控制台上。
     * 可能出现乱码。
     * */
    @org.junit.Test
    public void test3() {
        FileInputStream fis = null;

        try {
            //1. 创建相关的File类的对象
            File srcFile = new File("hello.txt");

            //2. 创建相关的字节流
            fis = new FileInputStream(srcFile);

            //3. 数据的读入和写出
            byte[] buffer = new byte[5];
            int len;//记录每次读入到buffer中字节的个数
            while ((len = fis.read(buffer)) != -1) {
                String str = new String(buffer,0,len);
                System.out.print(str);
            }
            System.out.println("复制成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4. 关闭资源
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }
}
